From 95d4397eefcf6fe4ce9036e0c8739012dc3e50d7 Mon Sep 17 00:00:00 2001 From: Michael Dale Date: Thu, 27 Aug 2009 18:26:51 +0000 Subject: [PATCH] * some js2 cleanup and refactoring for upload support * some minor work needed to finish cleaning up inline add media wizard upload across all upload modes (firefogg / normal http post) --- .../libAddMedia/mvBaseUploadInterface.js | 137 +++++++++++------- js2/mwEmbed/libAddMedia/mvFirefogg.js | 46 +++--- js2/mwEmbed/libAddMedia/remoteSearchDriver.js | 2 - js2/mwEmbed/libAddMedia/simpleUploadForm.js | 6 +- js2/mwEmbed/libEmbedVideo/embedVideo.js | 28 ++-- js2/mwEmbed/mv_embed.js | 4 +- js2/uploadPage.js | 27 ++-- 7 files changed, 133 insertions(+), 117 deletions(-) diff --git a/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js b/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js index 63c2bf838b..ada2eddb91 100644 --- a/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js +++ b/js2/mwEmbed/libAddMedia/mvBaseUploadInterface.js @@ -39,8 +39,8 @@ var default_bui_options = { 'done_upload_cb': null, 'target_edit_from':null, - //upload_mode can be 'post', 'api' or 'autodetect'. (autodetect issues an api call) - 'upload_mode': 'autodetect' + //default upload mode is 'api' but if no api_url will try tp post + 'upload_mode': 'api' } var mvBaseUploadInterface = function( iObj ){ @@ -72,13 +72,19 @@ mvBaseUploadInterface.prototype = { //set up the local pointer to the edit form: _this.editForm = _this.getEditForm(); if( _this.editForm ){ + + //if in api re-map the upload form to api: (we have to do this BEFORE the users selects a file) + if(_this.upload_mode == 'api'){ + _this.doRemapFormToApi(); + } + //set up the org_onsubmit if not set: if( typeof( _this.org_onsubmit ) == 'undefined' && _this.editForm.onsubmit ) _this.org_onsubmit = _this.editForm.onsubmit; //set up the submit action: $j( _this.editForm ).submit( function(){ - js_log('j.onSubmit'); + js_log('setupForm.onSubmit:'); //run the original onsubmit (if not run yet set flag to avoid excessive chaining ) if( typeof( _this.org_onsubmit ) == 'function' ){ if( ! _this.org_onsubmit() ){ @@ -92,6 +98,7 @@ mvBaseUploadInterface.prototype = { return true; } //get the input form data in flat json: + js_log('update formData::'); var tmpAryData = $j( _this.editForm ).serializeArray(); for(var i=0; i < tmpAryData.length; i++){ if( tmpAryData[i]['name'] ) @@ -105,10 +112,10 @@ mvBaseUploadInterface.prototype = { //for some unknown reason we have to drop down the #p-search z-index: $j('#p-search').css('z-index', 1); - //select upload mode: + //select upload mode: _this.detectUploadMode(); }catch(e){ - + js_log('::error in dispProgressOverlay or detectUploadMode'); } //don't submit the form we will do the post in ajax @@ -119,14 +126,15 @@ mvBaseUploadInterface.prototype = { }, detectUploadMode:function( callback ){ var _this = this; + js_log('detectUploadMode::' + _this.upload_mode); //check the upload mode: if( _this.upload_mode == 'autodetect' ){ js_log('detectUploadMode::' + _this.upload_mode + ' api:' + _this.api_url); if( ! _this.api_url ) return js_error( 'Error: can\'t autodetect mode without api url' ); do_api_req( { - 'data':{ 'action':'paraminfo','modules':'upload' }, - 'url' :_this.api_url + 'data': { 'action' : 'paraminfo', 'modules' : 'upload' }, + 'url' : _this.api_url }, function(data){ if( typeof data.paraminfo == 'undefined' || typeof data.paraminfo.modules == 'undefined' ) return js_error( 'Error: bad api results' ); @@ -153,6 +161,37 @@ mvBaseUploadInterface.prototype = { _this.doUploadSwitch(); } }, + //@@NOTE this could probably be depricated to just have a special:upload page that uses api keys? + // or maybe its usefull to seperate js and non-js submits + doRemapFormToApi:function(){ + var _this = this; + if( !_this.api_url ) + return false; + + //add the action api + $j(_this.editForm).attr('action', _this.api_url); + + //add api url + //add api action: + if( $j(_this.editForm).find("[name='action']").length == 0) + $j(_this.editForm).append(''); + + //add json format + if( $j(_this.editForm).find("[name='format']").length == 0) + $j(_this.editForm).append(''); + + //map a new hidden form + $j(_this.editForm).find("[name='wpUploadFile']").attr('name', 'file'); + $j(_this.editForm).find("[name='wpDestFile']").attr('name', 'filename'); + $j(_this.editForm).find("[name='wpUploadDescription']").attr('name', 'comment'); + $j(_this.editForm).find("[name='wpEditToken']").attr('name', 'token'); + $j(_this.editForm).find("[name='wpIgnoreWarning']").attr('name', 'ignorewarnings'); + $j(_this.editForm).find("[name='wpWatchthis']").attr('name', 'watch'); + + //update the status to 100% progress bar (no status in iframe submit) + $j('#up-progressbar' ).progressbar( 'value', parseInt( 100 ) ); + $j('#up-status-container').html( gM('mwe-upload-in-progress') ); + }, doUploadSwitch:function(){ var _this = this; js_log('mvUPload:doUploadSwitch():' + _this.upload_mode); @@ -165,60 +204,45 @@ mvBaseUploadInterface.prototype = { ); //do normal post _this.form_post_override = true; + js_log('doUploadSwitch:: submit call'); //do the submit : _this.editForm.submit(); }else if( - _this.upload_mode=='api' && + _this.upload_mode == 'api' && ( $j('#wpSourceTypeFile').length == 0 || $j('#wpSourceTypeFile').get(0).checked ) ){ //@@TODO check for sendAsBinnary to support firefox 3.5 progress - //set the form target to iframe target: - _this.iframeId = 'f_' + ($j('iframe').length + 1); - $j(_this.editForm).attr('target', _this.iframeId); - + //set the form target to iframe target: + _this.iframeId = 'f_' + ($j('iframe').length + 1); //add the iframe $j("body").append(''); + 'name="' + _this.iframeId + '" style="display:none;" >'); + $j(_this.editForm).attr('target', _this.iframeId); //set up the done binding $j('#' + _this.iframeId).load(function(){ _this.proccessIframeResult( $j(this).get(0) ); }); - - //set the editForm iframe target - //$j(_this.editForm).attr('target', id); - //set the action to the api url: $j(_this.editForm).attr('action', _this.api_url ); - //add api action: - if( $j(_this.editForm).find("[name='action']").length == 0) - $j(_this.editForm).append(''); - - //add json format - if( $j(_this.editForm).find("[name='format']").length == 0) - $j(_this.editForm).append(''); - - //map the form vars to api vars: - $j(_this.editForm).find("[name='wpUploadFile']").attr('name', 'file'); - $j(_this.editForm).find("[name='wpDestFile']").attr('name', 'filename'); - $j(_this.editForm).find("[name='wpUploadDescription']").attr('name', 'comment'); - $j(_this.editForm).find("[name='wpEditToken']").attr('name', 'token'); - $j(_this.editForm).find("[name='wpIgnoreWarning']").attr('name', 'ignorewarnings'); - $j(_this.editForm).find("[name='wpWatchthis']").attr('name', 'watch'); - - //update the status to 100% progress bar (no status in iframe submit) - $j('#up-progressbar' ).progressbar('value', parseInt( 100 ) ); - $j('#up-status-container').html( gM('mwe-upload-in-progress') ); - - js_log('do iframe form submit to: ' + $j(_this.editForm).attr('target')); - + + js_log('do iframe form submit to: ' + $j(_this.editForm).attr('target') + + ' destName:' + $j(_this.editForm).find("[name='filename']").val() ); + + //do post override _this.form_post_override = true; //reset the done with action flag: _this.action_done = false; - - _this.editForm.submit(); + + js_log('run editForm submit()'); + var tmpAryData = $j('#mw-upload-form' ).serializeArray(); + for(var i=0; i < tmpAryData.length; i++){ + if( tmpAryData[i]['name'] ) + js_log('name: ' + tmpAryData[i]['name'] + ' = ' + tmpAryData[i]['value']); + } + $j('#mw-upload-form').submit(); return false; }else if( _this.upload_mode == 'api' && $j('#wpSourceTypeURL').get(0).checked){ @@ -263,6 +287,7 @@ mvBaseUploadInterface.prototype = { } else if (doc.body){ // get the json str: json_str = $j(doc.body).find('pre').html(); + js_log('iframe:json::' + json_str + "\nbody:" + $j(doc.body).html() ); //htmlentties if (json_str) { response = window["eval"]("(" +json_str + ")"); @@ -329,14 +354,14 @@ mvBaseUploadInterface.prototype = { }); }, doAjaxUploadStatus:function() { - var _this = this; - + var _this = this; + //set up the progress display for status updates: _this.dispProgressOverlay(); var req = { - 'action' : 'upload', - 'httpstatus' : 'true', - 'sessionkey' : _this.upload_session_key + 'action' : 'upload', + 'httpstatus' : 'true', + 'sessionkey' : _this.upload_session_key }; //add token if present: if(this.etoken) @@ -540,6 +565,7 @@ mvBaseUploadInterface.prototype = { _this.warnings_sessionkey = apiRes.upload.warnings.sessionkey; var bObj = {}; bObj[ gM('mwe-ignorewarning') ] = function() { + js_log('ignorewarning req:') //re-inciate the upload proccess $j('#wpIgnoreWarning').attr('checked', true); $j( '#mw-upload-form' ).submit(); @@ -556,7 +582,7 @@ mvBaseUploadInterface.prototype = { }, processApiResult: function( apiRes ){ var _this = this; - js_log('processApiResult::'); + js_log('processApiResult::'); //check for upload api error: // {"upload":{"result":"Failure","error":"unknown-error","code":{"status":5,"filtered":"NGC2207%2BIC2163.jpg"}}} if( _this.apiUpdateErrorCheck(apiRes) === false){ @@ -637,6 +663,7 @@ mvBaseUploadInterface.prototype = { /*update to jQuery.ui progress display type */ dispProgressOverlay:function(){ var _this = this; + //remove old instance: if($j('#upProgressDialog').length!=0){ $j('#upProgressDialog').dialog( 'destroy' ).remove(); @@ -706,25 +733,27 @@ mvBaseUploadInterface.prototype = { */ $.fn.doDestCheck = function( opt ){ var _this = this; - var destFile = this.selector; + js_log('doDestCheck::' + _this.selector); + //set up option defaults; if(!opt.warn_target) opt.warn_target = '#wpDestFile-warning'; //empty target warn: - $j(opt.warn_target).empty(); - - //show loading - $j(destFile).after(''); + $j( opt.warn_target ).empty(); + + //show loading + $j( _this.selector ).append(''); + //try and get a thumb of the current file (check its destination) do_api_req({ 'data':{ - 'titles': 'File:' + $j(destFile).val(),//@@todo we may need a more clever way to get a the filename + 'titles': 'File:' + $j(_this.selector).val(),//@@todo we may need a more clever way to get a the filename 'prop': 'imageinfo', 'iiprop':'url|mime|size', 'iiurlwidth': 150 } - },function(data){ + },function(data){ //remove spinner: $j('#mw-spinner-wpDestFile').remove(); if(data && data.query && data.query.pages){ diff --git a/js2/mwEmbed/libAddMedia/mvFirefogg.js b/js2/mwEmbed/libAddMedia/mvFirefogg.js index 0665eefe6e..1add3c586a 100644 --- a/js2/mwEmbed/libAddMedia/mvFirefogg.js +++ b/js2/mwEmbed/libAddMedia/mvFirefogg.js @@ -442,7 +442,7 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface //check what mode to use firefogg in: if( _this.upload_mode == 'post' ){ _this.doEncode(); - }else if( _this.upload_mode == 'api' && _this.chunks_supported){ //if api mode and chunks supported do chunkUpload + }else if( _this.upload_mode == 'api' ){ //if api mode and chunks supported do chunkUpload _this.doChunkUpload(); }else{ js_error( 'Error: unrecongized upload mode: ' + _this.upload_mode ); @@ -450,36 +450,38 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface }, //doChunkUpload does both uploading and encoding at the same time and uploads one meg chunks as they are ready doChunkUpload : function(){ - js_log('doChunkUpload::'); + js_log('firefogg::doChunkUpload'); var _this = this; _this.action_done = false; + //extension should already be ogg but since its user editable, //check again //we are transcoding so we know it will be an ogg //(should not be done for passthrough mode) - var sf = _this.formData['wpDestFile']; + var sf = _this.formData['filename']; var ext = ''; if( sf.lastIndexOf('.') != -1){ ext = sf.substring( sf.lastIndexOf('.') ).toLowerCase(); } - if(!_this.encoder_settings['passthrough'] && $j.inArray(ext.substr(1), _this.ogg_extensions) == -1 ){ + if( !_this.encoder_settings['passthrough'] && $j.inArray(ext.substr(1), _this.ogg_extensions) == -1 ){ var extreg = new RegExp(ext + '$', 'i'); - _this.formData['wpDestFile'] = sf.replace(extreg, '.ogg'); + _this.formData['filename'] = sf.replace(extreg, '.ogg'); } //add chunk response hook to build the resultURL when uploading chunks //check for editToken: if(!this.etoken){ - if( _this.formData['wpEditToken']){ - _this.etoken = _this.formData['wpEditToken']; + js_log('missing token try ' + _this.formData['token']); + if( _this.formData['token']){ + _this.etoken = _this.formData['token']; _this.doChunkWithFormData(); }else{ get_mw_token( - 'File:'+ _this.formData['wpDestFile'], + 'File:'+ _this.formData['filename'], _this.api_url, function( eToken ){ if( !eToken || eToken == '+\\' ){ - _this.updateProgressWin(gM('fogg-badtoken'), gM('fogg-badtoken')); + _this.updateProgressWin( gM('fogg-badtoken'), gM('fogg-badtoken') ); return false; } _this.etoken = eToken; @@ -488,29 +490,30 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface ); } }else{ + js_log('we already have token: ' + this.etoken); _this.doChunkWithFormData(); } }, doChunkWithFormData:function(){ var _this = this; - js_log("doChunkWithFormData::" + _this.etoken); + js_log("firefogg::doChunkWithFormData" + _this.etoken); //build the api url: var aReq ={ 'action': 'upload', 'format': 'json', - 'filename': _this.formData['wpDestFile'], - 'comment': _this.formData['wpUploadDescription'], + 'filename': _this.formData['filename'], + 'comment': _this.formData['comment'], 'enablechunks': 'true' }; if( _this.etoken ) aReq['token'] = this.etoken; - if( _this.formData['wpWatchthis'] ) - aReq['watch'] = _this.formData['wpWatchthis']; + if( _this.formData['watch'] ) + aReq['watch'] = _this.formData['watch']; - if( _this.formData['wpIgnoreWarning'] ) - aReq['ignorewarnings'] = _this.formData['wpIgnoreWarning']; + if( _this.formData['ignorewarnings'] ) + aReq['ignorewarnings'] = _this.formData['ignorewarnings']; js_log('do fogg upload/encode call: '+ _this.api_url + ' :: ' + JSON.stringify( aReq ) ); js_log('foggEncode: '+ JSON.stringify( _this.encoder_settings ) ); @@ -562,7 +565,7 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface _this.formData[ 'wpSourceType' ] = 'upload'; _this.formData[ 'action' ] = 'submit'; //wpUploadFile is set by firefogg - delete _this.formData[ 'wpUploadFile' ]; + delete _this.formData[ 'file' ]; _this.fogg.post( _this.editForm.action, 'wpUploadFile', JSON.stringify( _this.formData ) ); //update upload status: @@ -606,7 +609,7 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface var apiResult = eval( response_text ); }catch(e){ var apiResult = null; - } + } } if(apiResult && _this.apiUpdateErrorCheck( apiResult ) === false){ //stop status update we have an error @@ -621,14 +624,13 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface //loop to get new status if still uploading (could also be encoding if we are in chunk upload mode) if( _this.fogg.state == 'encoding' || _this.fogg.state == 'uploading') { setTimeout(uploadStatus, 100); - }//check upload state else if( _this.fogg.state == 'upload done' || _this.fogg.state == 'done' || _this.fogg.state == 'encoding done' ) { //if in "post" upload mode read the html response (should be depricated): if( _this.upload_mode == 'api' ){ - if( apiResult.resultUrl ){ + if( apiResult && apiResult.resultUrl ){ var buttons ={}; buttons[gM('mwe-go-to-resource')] = function(){ window.location = apiResult.resultUrl; @@ -686,10 +688,10 @@ mvFirefogg.prototype = { //extends mvBaseUploadInterface procPageResponse:function( result_page ){ var _this = this; js_log('f:procPageResponse'); - var sstring = 'var wgTitle = "' + this.formData['wpDestFile'].replace('_',' '); + var sstring = 'var wgTitle = "' + this.formData['filename'].replace('_',' '); if(wgArticlePath){ - var result_txt = gM('mwe-upload_done', wgArticlePath.replace(/\$1/, 'File:' + _this.formData['wpDestFile'] ) ); + var result_txt = gM('mwe-upload_done', wgArticlePath.replace(/\$1/, 'File:' + _this.formData['filename'] ) ); }else{ result_txt = 'File has uploaded but api "done" URL was provided. Check the log for result page output'; } diff --git a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js index 0c6d3225aa..c54662768d 100644 --- a/js2/mwEmbed/libAddMedia/remoteSearchDriver.js +++ b/js2/mwEmbed/libAddMedia/remoteSearchDriver.js @@ -34,10 +34,8 @@ loadGM({ "mwe-results_from" : "Results from $2<\/a>", "mwe-missing_desc_see_source" : "This asset is missing a description. Please see the [$1 orginal source] and help describe it.", "rsd_config_error" : "Add media wizard configuration error: $1", - "mwe-your_recent_uploads" : "Your recent uploads", "mwe-upload_a_file" : "Upload a new file", - "mwe-resource_page_desc" : "Resource page description:", "mwe-edit_resource_desc" : "Edit wiki text resource description:", "mwe-local_resource_title" : "Local resource title:", diff --git a/js2/mwEmbed/libAddMedia/simpleUploadForm.js b/js2/mwEmbed/libAddMedia/simpleUploadForm.js index 6a00a0e1c4..00d9237980 100644 --- a/js2/mwEmbed/libAddMedia/simpleUploadForm.js +++ b/js2/mwEmbed/libAddMedia/simpleUploadForm.js @@ -55,10 +55,10 @@ var default_form_options = { //form name set: '
'+ - '
' + + '
' + '
'+ - '
'+ + '
'+ '
' + '